package com.gy.paidao.person;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;

import com.gy.paidao.BaseActivity;
import com.example.mapdemo.R;
import com.gy.paidao.bean.BaseBean;
import com.gy.paidao.util.AnimUtil;
import com.gy.paidao.util.KeyBoard;
import com.gy.paidao.util.Urls;
import com.jky.struct2.http.core.AjaxParams;

/**
 * @author GY
 * @date 16/3/23
 * @Description:注册完验证邮箱
 */
public class RegisterEmailActivity extends BaseActivity {

    private TextView tvEmail, tvSend, tvCheck;
    private EditText etCode;
    private String strEmail, strCode;
    private int userId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register_email);
        setViews();
    }

    @Override
    protected void initVariable() {
        userId = getIntent().getIntExtra("userId", 0);
        strEmail = getIntent().getStringExtra("email");
    }

    @Override
    protected void setTitleViews() {

    }

    @Override
    protected void setViews() {
        tvEmail = (TextView) findViewById(R.id.register_email_tv_email);
        tvSend = (TextView) findViewById(R.id.register_email_tv_send);
        tvCheck = (TextView) findViewById(R.id.register_email_tv_check);
        etCode = (EditText) findViewById(R.id.register_email_et_code);

        tvSend.setOnClickListener(this);
        tvCheck.setOnClickListener(this);

        tvEmail.setText(strEmail + "");

        etCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!TextUtils.isEmpty(s.toString())) {
                    tvCheck.setBackgroundResource(R.drawable.shape_input_blue_bg);
                    tvCheck.setTextColor(RegisterEmailActivity.this.getResources().getColor(R.color.white));
                } else {
                    tvCheck.setTextColor(0xff8dbedd);
                    tvCheck.setBackgroundResource(R.drawable.shape_input_blue_alpha_bg);
                }
            }
        });
    }

    @Override
    protected void doClickAction(int viewId) {
        super.doClickAction(viewId);
        switch (viewId) {
            case R.id.register_email_tv_send:
                requestSendVeriCode();
                break;
            case R.id.register_email_tv_check:
                strCode = etCode.getText().toString().trim();
                if (TextUtils.isEmpty(strCode)) {
                    showToast("请输入验证码");
                } else {
                    KeyBoard.demissKeyBoard(this, etCode);
                    requestVeriCode();
                }
                break;
        }
    }

    /** 验证验证码*/
    private void requestVeriCode() {
        showLoading();
        params = new AjaxParams();
        params.put("email", strEmail + "");
        params.put("verificationCode", strCode + "");
        httpRequest.get(Urls.url_vericode, params, callBack, 0);
    }

    /** 重新发送验证码*/
    private void requestSendVeriCode() {
        showLoading();
        params = new AjaxParams();
        params.put("email", strEmail + "");
        httpRequest.get(Urls.url_sendVeriCode, params, callBack, 1);
    }

    @Override
    protected void handlerBean(int requestCode, BaseBean baseBean) {
        super.handlerBean(requestCode, baseBean);
        if (baseBean.success) {
            switch (requestCode) {
                case 0:
                    intent = new Intent(this, FinishProfileActivity.class);
                    intent.putExtra("userId",userId);
                    intent.putExtra("email",strEmail);
                    intent.putExtra("code",strCode);
                    startActivityForResult(intent,101);
                    AnimUtil.pushLeftInAndOut(this);
                    break;
                case 1:
                    showToast("发送成功  请注意查收");
                    break;
            }
        } else {
            if (!TextUtils.isEmpty(baseBean.errMsg)) {
                showToast(baseBean.errMsg);
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == 101){
            setResult(101);
            finish();
        }
    }
}
